package com.hanrui.android.library.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hanrui.android.R;
import com.hanrui.android.constant.LoadType;
import com.hanrui.android.library.widget.TitleBar;

import java.util.List;


public abstract class ActionBarBaseActivity<T extends BaseContract.BasePresenter> extends ImmersionBaseActivity<T> {

    private SwipeRefreshLayout swipeLayout;
    private LinearLayout ll_right_root;
    public TitleBar mTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);

        swipeLayout =  findViewById(R.id.swipeLayout);
        ll_right_root =  findViewById(R.id.ll_right_root);
        mTitleBar =  findViewById(R.id.main_title_bar);

        mTitleBar.setImmersive(true);
        mTitleBar.setTitleColor(ContextCompat.getColor(this,R.color.white));
        mTitleBar.setBackgroundColor(ContextCompat.getColor(this,R.color.c_2795ff));
        mTitleBar.setLeftTextColor(ContextCompat.getColor(this,R.color.white));

        mTitleBar.setLeftImageResource(R.mipmap.left_back);
        mTitleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        swipeLayout.setColorSchemeColors(ContextCompat.getColor(this,R.color.c_2795ff));
        swipeLayout.setEnabled(false);

    }


    public View getNoDataView(RecyclerView mRecyclerView){
        return getLayoutInflater().inflate(R.layout.emptyview, (ViewGroup) mRecyclerView.getParent(), false);
    }

    /**
     * 2.重写setContentView()函数,自定义行为，让View添加到 mLlRoot容器中，
     * 而不添加到content view区域
     *
     * @param layoutResID 资源ID
     */
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, ll_right_root, false);
        ll_right_root.addView(view,0);
    }


    /**
     * 是否需要下拉刷新
     * @param b
     */
    public void setIsPullRefresh(boolean b){
        swipeLayout.setEnabled(b);
    }

    /**
     * 关闭刷新
     */
    public void closeResresh(){
        swipeLayout.setRefreshing(false);
    }

    /**
     * 设置下拉刷新监听
     * @param listener
     */
    public void setRefreshListener(SwipeRefreshLayout.OnRefreshListener listener){
        swipeLayout.setOnRefreshListener(listener);
    }

    /**
     * 设置刷新动画
     */
    public void setRefreshing(){
        swipeLayout.setRefreshing(true);
    }


    /**
     * 设置加载数据结果
     *
     * @param baseQuickAdapter
     * @param list
     * @param loadType
     */
    public void setLoadDataResult(BaseQuickAdapter baseQuickAdapter, List list, @LoadType.checker int loadType, boolean isLoadMoreEnd) {
        switch (loadType) {
            case LoadType.TYPE_REFRESH_SUCCESS:
                baseQuickAdapter.setNewData(list);
                closeResresh();
                baseQuickAdapter.setEnableLoadMore(true);
                break;
            case LoadType.TYPE_REFRESH_ERROR:
                closeResresh();
                baseQuickAdapter.setEnableLoadMore(true);
                break;
            case LoadType.TYPE_LOAD_MORE_SUCCESS:
                if (list != null) baseQuickAdapter.addData(list);
                if (isLoadMoreEnd) {
                    baseQuickAdapter.loadMoreComplete();
                }else {
                    baseQuickAdapter.loadMoreEnd();
                }
                baseQuickAdapter.setEnableLoadMore(true);
                break;
            case LoadType.TYPE_LOAD_MORE_ERROR:
                baseQuickAdapter.loadMoreFail();
                baseQuickAdapter.setEnableLoadMore(true);
                break;
        }
    }
}
